package com.yuxinmuyu.sports_management.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuxinmuyu.sports_management.bean.entiy.SStudent;
import com.yuxinmuyu.sports_management.bean.entiy.SysUsers;
import com.yuxinmuyu.sports_management.bean.request.GetSStudentRequest;
import com.yuxinmuyu.sports_management.bean.request.GetSelectStudentRequest;
import com.yuxinmuyu.sports_management.bean.request.StudentIDRequest;
import com.yuxinmuyu.sports_management.bean.request.UpdateStudentRequest;
import com.yuxinmuyu.sports_management.bean.response.sStudentPageResponse;
import com.yuxinmuyu.sports_management.exception.BusinessException;
import com.yuxinmuyu.sports_management.mapper.SStudentMapper;
import com.yuxinmuyu.sports_management.mapper.UserMapper;
import com.yuxinmuyu.sports_management.service.SStudentService;
import com.yuxinmuyu.sports_management.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
public class SStudentServiceImpl implements SStudentService{
    @Resource
    SStudentMapper sStudentMapper;
    @Resource
    UserMapper userMapper;
    @Override
    public Page<SStudent> getStudentInfo(GetSStudentRequest request) {
        Page<SStudent> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        page = sStudentMapper.getPage(page, request.getEntryYear(), request.getStudName());
        return page;
    }

    @Override
    public Page<sStudentPageResponse> selectStudent(GetSelectStudentRequest request) {
        Page<sStudentPageResponse> page = new Page<>(request.getCurrentPage(), request.getPageSize());
        page = sStudentMapper.selectStudent(page, request.getEntryYear(),request.getDeptID(),request.getMajorID(),request.getClassID(),request.getKeyword());
        return page;
    }

    @Transactional( rollbackFor = Exception.class)
    @Override
    public void deleteStudent(StudentIDRequest request) {
        int n=sStudentMapper.deleteById(request.getStuID());
        if(n==0){
            throw new BusinessException("删除失败！");
        }
        n=userMapper.deleteById(request.getStuID());
        if(n==0){
            throw new BusinessException("删除失败！");
        }
    }

    @Override
    public sStudentPageResponse selectStudentByID(StudentIDRequest request) {
        return sStudentMapper.selectByStudId(request.getStuID());
    }

    @Override
    public void addStudent(SStudent student) {
        SStudent sStudent = sStudentMapper.selectById(student.getStuID());
        if(Objects.nonNull(sStudent)){
            throw new BusinessException("已经存在该信息！");
        }
        student.setOperator(UserUtil.getUserName());
        student.setLastDate(new Date());
        int n = sStudentMapper.insert(student);
        if (n==0){
            throw new BusinessException("添加失败！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStudent(UpdateStudentRequest student) {
        int n=sStudentMapper.update(null, Wrappers.<SStudent>lambdaUpdate().eq(
                        SStudent::getStuID,student.getStuID())
                .set(SStudent::getEntryYear,student.getEntryYear())
                .set(SStudent::getClassID,student.getClassID())
                .set(SStudent::getMajorID,student.getMajorID())
                .set(SStudent::getDeptID,student.getDeptID())
                .set(SStudent::getStudName,student.getStudName())
                .set(SStudent::getStudBirth,student.getStudBirth())
                .set(SStudent::getStudSex,student.getStudSex())
                .set(SStudent::getPhoneNumber,student.getPhoneNumber())
                .set(SStudent::getEmail,student.getEmail())
                .set(SStudent::getStudNation,student.getStudNation())
                .set(SStudent::getStudNative,student.getStudNative())
                .set(SStudent::getIDCard,student.getIDCard())
                .set(SStudent::getStudMemo,student.getStudMemo())
                .set(SStudent::getLastDate,new Date())
                .set(SStudent::getOperator,UserUtil.getUserName())
                .set(SStudent::getStudState,student.getStudState())
        );
        if(n==0){
            throw new BusinessException("修改ID为"+student.getStuID()+"的学生信息失败!");
        }
        n=userMapper.update(null,Wrappers.<SysUsers>lambdaUpdate().eq(SysUsers::getUserID,student.getStuID())
                .set(SysUsers::getAvater,student.getAvater()));
        if(n==0){
            throw new BusinessException("修改ID为"+student.getStuID()+"的学生信息失败!");
        }
    }
}
